Libraries

There is a problem with dplyr and plotly. When loaded tofether both have functions of same name so must point to the library you reall need, i.e. dplyr::select()

#library(reticulate)
library(tidyverse)
library(plotly)
library(janitor)
library(readxl)
library(xlsx)

Download the DK dataset for today

    Remove the players that are out or on the injured reserve
player_projection <- read_csv("DFF_NHL_cheatsheet.csv", col_names = TRUE) %>%
 dplyr::filter(!injury_status %in% c("O", "IR")) %>% # remove the out and injured reserve players
 dplyr::mutate(salary_factor = salary*.001, range_factor = (L5_ppg_max+L5_ppg_floor)*.6, total_score = salary_factor + range_factor + ppg_projection + value_projection, pick = "x" )

Select a line based on max ppg totals

Clean Up and filtering

      slice takes the top 3 grouped records
      later I ungroup and combine field names.
      
      Then I filter out points above 60 to create a new object for graphing
team__reg_line <- player_projection %>%
  clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  filter(!position == "G") %>%

  select(team, reg_line, total_score, salary) %>%
  arrange(team, reg_line, desc(total_score)) %>%
  group_by(team, reg_line) %>%
  dplyr::slice(1:3) %>%
  dplyr::summarise(total_pts = sum(total_score), salary = sum(salary)) %>%
  dplyr::ungroup() %>%
  unite(team_line, team, reg_line, sep = "_", remove = TRUE) 
## `summarise()` has grouped output by 'team'. You can override using the
## `.groups` argument.
team_pts_max_20 <- team__reg_line %>%
  dplyr::arrange(desc(total_pts)) %>%
  dplyr:: slice(1:20) # slice the top 20 records

Use ggplot to graph

    this is called a lollypop chart
# install.packages("ggplot2")
library(ggplot2)

ggplot(team_pts_max_20, aes(x = team_line, y = salary)) +
  geom_segment(aes(x = reorder(team_line, total_pts), xend = team_line, y = 5000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = round(total_pts)), color = "white", size = 3) 

## Use plotly with ggplot2

p <- ggplot(team_pts_max_20, aes(x = team_line, y = salary)) +
  geom_segment(aes(x = reorder(team_line, total_pts), xend = team_line, y = 5000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = round(total_pts)), color = "white", size = 3) 

ggplotly(p)

GOALIE SELECTION

Start Working on selecting a Goalie

Review choices for goalie bases on DK Spreadsheet
      Filter for Goalies only
      Reduce data elements
      sort descending value
      create a variance/spread column replacing l5_
      create new field names for connecting to other databases 
goalies <- player_projection %>%
  clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  filter(position == "G") %>%
  select(1:3, 5:6, 8, 10:17, 18:20, 23:26) %>%
  arrange(desc(total_score), desc(salary)) %>%
  unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
  unite(name_team, last_name, team, sep = "_", remove = FALSE)


goalie_select_10 <- goalies %>%
  head(10)

Create a chart that looks at 10 top value score selections based on value scores

# install.packages("ggplot2")
library(ggplot2)

ggplot(goalie_select_10, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 5000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +

geom_text(aes(label = total_score), color = "white", size = 3) 

Create the Goalie Selection chart in plotly

p <- ggplot(goalie_select_10, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 5000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 


ggplotly(p)

CENTER SELECTION

Now find some centers

centers <- player_projection %>%
  clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  filter(position == "C") %>%
  select(1:3, 5:6, 8, 10:17, 18:20, 23:26) %>%
  arrange(desc(total_score), desc(salary)) %>%
  unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
  unite(name_team, last_name, team, sep = "_", remove = FALSE)

center_select_15 <- centers %>%
  head(15)

Create a chart that looks at 15 top value score selections based on value scores

# install.packages("ggplot2")
library(ggplot2)

ggplot(center_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 

Create the Center Selection chart in plotly

p <- ggplot(center_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 


ggplotly(p)

WINGS SELECTION

Now find some wingmen

wings <- player_projection %>%
  clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  filter(position == "W") %>%
  select(1:3, 5:6, 8, 10:17, 18:20, 23:26) %>%
  arrange(desc(total_score), desc(salary)) %>%
  unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
  unite(name_team, last_name, team, sep = "_", remove = FALSE)


wing_select_15 <- wings %>%
  head(15)

Create a chart that looks at 15 top value score selections

# install.packages("ggplot2")
library(ggplot2)

ggplot(wing_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 

## Create the wing Selection chart in plotly

p <- ggplot(wing_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 


ggplotly(p)

DEFENSEMEN SELECTION

Now find some defensemen based on value scores

defense <- player_projection %>%
  clean_names() %>%
  mutate_if(is.numeric, ~replace_na(., 0)) %>%
  filter(position == "D") %>%
  select(1:3, 5:6, 8, 10:17, 18:20, 23:26) %>%
  arrange(desc(total_score), desc(salary)) %>%
  unite(name, first_name, last_name, sep = " ", remove = FALSE) %>%
  unite(name_team, last_name, team, sep = "_", remove = FALSE)

defense_select_15 <- defense %>%
  head(15)

Create a chart that looks at 15 top value score selections

# install.packages("ggplot2")
library(ggplot2)

ggplot(defense_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 

## Create the defensemen Selection chart in plotly

p <- ggplot(defense_select_15, aes(x = name_team, y = salary)) +
  geom_segment(aes(x = reorder(name_team, total_score), xend = name_team, y = 2000, yend = salary)) +
  geom_point() +
  coord_flip() +
  geom_point(size = 12, pch = 21, bg = 4, col = 1) +
  geom_text(aes(label = total_score), color = "white", size = 3) 


ggplotly(p)

Combine player data frames

# merge files and move pick column near names
combo_player <- do.call("rbind", list(goalies, centers, wings, defense)) %>%
  dplyr::relocate(pick, name, name_team, position, team, opp, reg_line, pp_line, spread, salary, total_score)

  
  


write_csv(combo_player, "combo_player.csv")

# Write the dataset to excel  (OLD CODE)
#write.xlsx(combo_player, file = "combo_player.xlsx", sheetName = "combo_player", append = FALSE)



# Write the dataset to excel
combo_player <- as.data.frame(combo_player)
write.xlsx(combo_player,
           file = paste("extra/My_Picks/combo_player_", Sys.Date(),
                        ".xlsx", sep = ""),
           sheetName = "combo_player", row.names = FALSE , append = FALSE)